%%%---------------------------------------------------------------------------------------
%%% @author     Stuart Jackson <simpleenigma@gmail.com> [http://erlsoft.org]
%%% @copyright  2006 - 2007 Simple Enigma, Inc. All Rights Reserved.
%%% @doc        General ERML behavior
%%% @reference  See <a href="http://erlsoft.org/modules/erlweb" target="_top">Erlang Software Framework</a> for more information
%%% @reference See <a href="http://erlweb.googlecode.com" target="_top">ErlWeb Google Code Repository</a> for more information
%%% @version    0.0.2
%%% @since      0.0.2
%%% @end
%%%
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2007 Stuart Jackson, Simple Enigma, Inc. All Righs Reserved
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%
%%%
%%%---------------------------------------------------------------------------------------
-module(gen_erml).
-author('sjackson@simpleenigma.com').

-include_lib("erlweb/include/erml.hrl").
-include_lib("xmerl/include/xmerl.hrl").

-export([get_attrib/3,get_content/2,get_value/2,get_value/3]).
-export([get_xml_value/1,get_xml_attribute/2,get_xml_attrib_value/2]).
-export([behaviour_info/1]).

behaviour_info(callbacks) -> [];
behaviour_info(_Other) -> undefined.





get_attrib(Element,ERML,List) -> 
	get_attrib(Element,ERML,List,[]).


get_attrib(_Element,_ERML,[],Acc) -> lists:reverse(Acc);
get_attrib(Element,ERML,[H|T],Acc) ->
	case lists:keysearch(H,2,Element#xmlElement.attributes) of
		{value,Attrib} ->
			Value = Attrib#xmlAttribute.value,
			{NewXML,NewERML} = erml:process_string(Value,ERML),
			[Content] = NewXML#xmlElement.content,
			NewValue = Content#xmlText.value,
			get_attrib(Element,NewERML,T,[{H,NewValue}|Acc]);
		_ -> get_attrib(Element,ERML,T,[{H,undefined}|Acc])
	end.


get_content(Element,ERML) ->
	Content = Element#xmlElement.content,
	case Content of
		[#xmlText{value = Value}] -> 
			{NewXML,NewERML} = erml:process_string(Value,ERML),
			[NewContent] = NewXML#xmlElement.content,
			NewValue = NewContent#xmlText.value,
			{NewValue,NewERML};
		_ -> {Content,ERML}
	end.

get_value(Key,Args) -> get_value(Key,Args,undefined).
get_value(Key,Args,Default) ->
	case lists:keysearch(Key,1,Args) of
		{value,{Key,Value}} -> 
			case Value of
				undefined -> Default;
				Value -> Value
			end;
		_ -> Default
	end.





get_xml_attrib_value(Element,Name) -> get_xml_value(get_xml_attribute(Element,Name)).

get_xml_attribute(Element,Name) ->
	case lists:keysearch(Name,2,Element#xmlElement.attributes) of
		{value,Attrib} -> Attrib;
		_ -> false
	end.

get_xml_value(false) -> [];
get_xml_value(Attrib) when is_record(Attrib,xmlAttribute) -> Attrib#xmlAttribute.value.



